package handlers

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"pool/pool_backend/internal/service"
	"pool/pool_backend/pkg/logger"
)

// UserHandler 用户处理器
type UserHandler struct {
	userService service.UserService
}

// NewUserHandler 创建用户处理器
func NewUserHandler(userService service.UserService) *UserHandler {
	return &UserHandler{
		userService: userService,
	}
}

// BindInviter 绑定邀请人地址
func (h *UserHandler) BindInviter(c *gin.Context) {
	var req struct {
		InviterAddress string `json:"inviterAddress" binding:"required"`
	}

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "Invalid request: " + err.Error(),
		})
		return
	}

	// 从请求上下文中获取用户地址
	// userAddress := c.GetString("address")
	// if userAddress == "" {
	// 	c.JSON(http.StatusUnauthorized, gin.H{
	// 		"code":    http.StatusUnauthorized,
	// 		"message": "Unauthorized",
	// 	})
	// 	return
	// }
	// 从请求字段中获取用户地址
	userAddress := c.Param("address")
	// 判断是否已经绑定过邀请人地址
	binded,err := h.userService.IsBinded(userAddress)
	
	if err != nil {
		logger.ErrorF("Failed to check if user is binded: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "Failed to check if user is binded: " + err.Error(),
		})
		return
	}
	if binded {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "User already bound to an inviter",
		})
		return
	}
	// 绑定邀请人
	err = h.userService.BindInviter(userAddress, req.InviterAddress)
	if err != nil {
		logger.ErrorF("Failed to bind inviter: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "Failed to bind inviter: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "The binding of the inviter's address was successful",
	})
}

// GetUserInfo 获取用户信息
func (h *UserHandler) GetUserInfo(c *gin.Context) {
	// 从路径参数中获取地址
	address := c.Param("address")
	if address == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "Address is required",
		})
		return
	}

	// 获取用户信息
	userInfo, err := h.userService.GetUserInfo(address)
	if err != nil {
		logger.ErrorF("Failed to get user info: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "Failed to get user info: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "Successfully obtained the user information",
		"data":    userInfo,
	})
}

// GetSubUsers 获取用户下级信息
func (h *UserHandler) GetSubUsers(c *gin.Context) {
	// 从路径参数中获取地址
	address := c.Param("address")
	if address == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "Address is required",
		})
		return
	}

	// 获取下级用户
	subUsers, err := h.userService.GetSubUsers(address)
	if err != nil {
		logger.ErrorF("Failed to get sub users: %v", err)
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "Failed to get sub users: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "get sub successful",
		"data":    subUsers,
	})
}
func (h *UserHandler) GetData(c *gin.Context){
	// TODO：获取链上链下数据
	address := c.Param("address")
	if address == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "Address is required",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "get data successful",
		"data":    gin.H{
			"RewardAmount": 123,
			"InterActiveCount": 123,
			"BusinessDay": 123,
			"ReciveAirdrop": 123,
			"InviteReward": 123,
			"AirdropProgress": 12.34,
		},
	})
}